package com.spark.tags;

import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.xml.sax.SAXException;

import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.core.XMapParser;
import com.spark.util.IDGenerator;

public class NavBar extends ToolBar {
	
	public static final String TAG_NAME = "navBar";
	public static final String PROPERTITY_DATASET = "dataSet";
	public static final String PROPERTITY_PAGE_SIZE_EDITABLE = "enablepagesize";
	public static final String PROPERTITY_NAVBAR_TYPE = "navType";
	
	private static final String DEFAULT_TEMPLATES = "navBar.ftl";
	
	protected int getDefaultHeight(){
		return 26;
	}
	
	
	public String getTemplate() {
		return DEFAULT_TEMPLATES;
	}
	
	
	public void init(XMap view, SparkContext context) throws Exception {
		
		try {
			String id = view.getString(PROPERTITY_ID, "");
			if("".equals(id)) {
				id = IDGenerator.getInstance().generate();
			}
			view.putString(PROPERTITY_ID, id);
			
			String type = view.getString(NavBar.PROPERTITY_NAVBAR_TYPE,"complex");
			String dataset = view.getString(PROPERTITY_DATASET);
			context.addJsonConfig(PROPERTITY_DATASET, dataset);
			context.addJsonConfig(PROPERTITY_NAVBAR_TYPE, type);
			if("simple".equalsIgnoreCase(type)){
				view.put(PROPERTITY_CLASSNAME, view.getString(PROPERTITY_CLASSNAME)+" simple-navbar");
				createSimpleNavBar(view, context,dataset);
			}else{
				createComplexNavBar(view, context,dataset);
			}
		} catch (SAXException e) {
			throw new IOException(e.getMessage());
		}
		super.init(view, context);
	}
	
	private void createSimpleNavBar(XMap view,SparkContext context,String dataset) throws Exception{
		String pageInfoText = "<div stype='displayInfo' class='nav-label' style='float:right;'></div>";
		XMap pageInfo = XMapParser.parseReader(new StringReader(pageInfoText));
		view.addChild(pageInfo);
	}
	
	private void createComplexNavBar(XMap view,SparkContext context,String dataset) throws Exception{
		StringBuilder fpsb = new StringBuilder();
		fpsb.append("function(){$('").append(dataset).append("').firstPage()}");
		view.addChild(createButton("nav-firstpage",fpsb.toString(),"第一页"));
		
		StringBuilder ppsb = new StringBuilder();
		ppsb.append("function(){$('").append(dataset).append("').prePage()}");
		view.addChild(createButton("nav-prepage",fpsb.toString(),"上一页"));
		view.addChild(createSeparator());
		
		String pagetext = "<div class='s_label' stype='currentPage' style='margin:2px 3px 0px 3px'>&#160;</div>";
		XMap pageinfo = XMapParser.parseReader(new StringReader(pagetext));
		view.addChild(pageinfo);
		
		
		
		String inputId = IDGenerator.getInstance().generate();
		context.addJsonConfig("inputId", inputId);
		XMap pageField = new XMap("numberField");
		pageField.setNameSpaceURI(DEFAULT_NAME_SPACE);
		pageField.putBoolean(NumberField.PROPERTITY_ALLOWDECIMALS, false);
		pageField.putBoolean(NumberField.PROPERTITY_ALLOWNEGATIVE, false);
		pageField.putString(PROPERTITY_ID, inputId);
		pageField.putInt(PROPERTITY_WIDTH, 26);
		pageField.putInt(PROPERTITY_HEIGHT, 20);
		view.addChild(pageField);
		
		String text = "<div class='s_label' stype='pageInfo' style='margin:2px 3px 0px 3px'>    </div>";
		XMap totalpage = XMapParser.parseReader(new StringReader(text));
		view.addChild(totalpage);
		
		view.addChild(createSeparator());
		StringBuilder npsb = new StringBuilder();
		npsb.append("function(){$('").append(dataset).append("').nextPage()}");
		view.addChild(createButton("nav-nextpage",npsb.toString(),"下一页"));
		StringBuilder lpsb = new StringBuilder();
		lpsb.append("function(){$('").append(dataset).append("').lastPage()}");
		view.addChild(createButton("nav-lastpage",lpsb.toString(),"最后一页"));
		StringBuilder rfsb = new StringBuilder();
		rfsb.append("function(){$('").append(dataset).append("').query($('").append(dataset).append("').currentPage)}");
		view.addChild(createButton("nav-refresh",rfsb.toString(),"刷新"));
		view.addChild(createSeparator());
		
		if(view.getBoolean(PROPERTITY_PAGE_SIZE_EDITABLE, true)){
			String pageSizeInfo="<div class='s_label' stype='pageSizeInfo' style='margin:2px 3px 0px 3px'>    </div>";
			XMap pagesize = XMapParser.parseReader(new StringReader(pageSizeInfo));
			view.addChild(pagesize);
			String comboBoxId = IDGenerator.getInstance().generate();
			context.addJsonConfig("comboBoxId", comboBoxId);
			XMap comboBox = new XMap("comboBox");
			comboBox.setNameSpaceURI(DEFAULT_NAME_SPACE);
			comboBox.putString(PROPERTITY_ID, comboBoxId);
			comboBox.putInt(PROPERTITY_WIDTH, 50);
			comboBox.putInt(PROPERTITY_HEIGHT, 20);
			view.addChild(comboBox);
			String pageSizeInfo2="<div class='s_label' stype='pageSizeInfo2' style='margin:2px 3px 0px 3px'>    </div>";
			XMap pagesize2 = XMapParser.parseReader(new StringReader(pageSizeInfo2));
			view.addChild(pagesize2);
			view.addChild(createSeparator());
		}
		String pageInfoText = "<div stype='displayInfo' class='s_label' style='float:right;'></div>";
		XMap pageInfo = XMapParser.parseReader(new StringReader(pageInfoText));
		view.addChild(pageInfo);
	}
	
	private XMap createSeparator(){
		XMap sep = new XMap("separator");
		sep.setNameSpaceURI(DEFAULT_NAME_SPACE);
		return sep;
	}
	
	private XMap createButton(String clz,String function, String title){
		XMap button = new XMap(XButton.TAG_NAME);
		button.setNameSpaceURI(DEFAULT_NAME_SPACE);
		button.put(PROPERTITY_WIDTH, new Integer(20));
		button.put(PROPERTITY_HEIGHT, new Integer(22));
		button.put(XButton.PROPERTITY_TEXT_CLASS, clz);
		button.put(XButton.PROPERTITY_TITLE, title);
		if(!"".equals(function))button.put(XButton.PROPERTITY_CLICK, function);
		return button;
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<DIV class=\"").append(get(map,"className")).append("\" id=\"").append(get(map,"id")).append("\" style=\"height:").append(get(map,"height")).append("px;width:").append(get(map,"width")).append("px;").append(get(map,"style")).append("\">\r");
		sb.append("	<DIV style=\"padding-left:4px;padding-right:4px;\">").append(get(map,"items")).append("</DIV>\r");
		sb.append("</DIV>\r");
		sb.append("<SCRIPT>new Spark.NavBar(").append(get(map,"config")).append(");</SCRIPT>\r");
		return sb.toString();
	}
	
}
